<?php

class SurfController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','medallero','resultados'),
				'users'=>array('*'),
			),		
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('marcador','eliminar','asignacion','editar','inicio','guardar'),
				'users'=>Yii::app()->getModule('user')->getAdmins(),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionMarcador()
	{
		$this->render('index');
	}
	
	public function actionResultados()
	{
		Yii::app()->theme='resultados';	
		
		Yii::app()->theme='resultados';	
		$connection = Yii::app()->db;	
		$command = $connection->createCommand("SELECT grsm.surf_marcador_id,grsm.grid_programa_entidad_id,gre.grid_grupo,grsm.color,
        grsm.puntaje,grsm.posicion,grsm.estado,grp.grid_programa_descripcion,gre.grid_grupo,gpa.pais_nombre
        FROM grid_r_surf_marcador grsm
        inner join grid_r_entidad gre on grsm.grid_programa_entidad_id = gre.grid_entidad_id 
        inner join grid_r_programa_entidad grpe on grpe.grid_entidad_id = gre.grid_entidad_id 
        inner join grid_r_programa grp on grp.grid_programa_id = grpe.grid_programa_id
        inner join grid_pais gpa on gpa.pais_id = gre.grid_pais_id
        where gre.grid_deporte_actividad_id =3 and estado='TERMINADO'
        
        order by  grid_programa_descripcion,posicion
      ");

		$dataReader = $command->query();
		
		$rows=$dataReader->readAll();
		
		$this->renderPartial('surf',array('marcadores'=>$rows));
	}
	
	
	public function actionIndex()
	{
		$this->render('index');
	}

	public function actionMedallero()
	{
		$this->render('medallero');
	}
	
	
	/***Modelo a seguir ***/
public function actionEliminar()
	{
	
		$DEId = $_POST['DEId'];
  		
  		$connection = Yii::app()->db;
		$command = $connection->createCommand("
		   DELETE FROM grid_r_surf_marcador WHERE surf_marcador_id='{$DEId}';");
		$command->query();			
	}
	
	public function actionAsignacion()
	{
		
		if(isset($_POST['deporte'])&&isset($_POST['entidad'])&&isset($_POST['programa']))
		{
		
		$deporte = $_POST['deporte'];
		$entidad = $_POST['entidad'];
		$programa = $_POST['programa'];
		
		if(!empty($deporte)&&!empty($entidad)&&!empty($programa))
			$this->render('asignacion',array('deporte'=>$deporte,'entidad'=>$entidad,'programa'=>$programa));
		else 
			$this->render('inicio');
		}else
			$this->render('inicio');

	}

	public function actionEditar()
	{
		$gmid = Yii::app()->request->getQuery('gmid');
		$deporte = Yii::app()->request->getQuery('deporte');
		$entidad = Yii::app()->request->getQuery('entidad');
		$programa = Yii::app()->request->getQuery('programa');				
	
		if(!empty($deporte)&&!empty($entidad)&&!empty($programa)&&!empty($gmid))	
		{	
			$oMedalla = RSurfMarcador::model()->findByPk($gmid);
			
			$this->render('editar',array('deporte'=>$deporte,'medalla'=>$oMedalla));	
		}		
		else 
			$this->render('inicio');
	}
	
	
	public function actionInicio()
	{
		$this->render('inicio');
	}
	
	public function actionGuardar()
	{
		if(isset($_POST['Medalla']))
		{
			$medalla = new RSurfMarcador();
			
			if(!empty($_POST['Medalla']['surf_marcador_id']))
			{
				
				$medalla=  $this->loadModel($_POST['Medalla']['surf_marcador_id']);
				$medalla->attributes=$_POST['Medalla'];
			}
			else
			{
				$medalla->grid_programa_entidad_id=$_POST['Medalla']['grid_programa_entidad_id'];
				$medalla->color=$_POST['Medalla']['color'];
				$medalla->puntaje=$_POST['Medalla']['puntaje'];
				$medalla->posicion=$_POST['Medalla']['posicion'];
				$medalla->estado=$_POST['Medalla']['estado'];				
			}
			
			$error=0;
			$mensaje='';
			
			if($medalla->validate())
			{				
				$medalla->save();
				$mensaje='Marcador Registrado con Éxito.';
			}	
			else{				
				$mensaje=$medalla->getErrors();	
				$error=1;		
			}
			
			$this->render('guardar',array('mensaje'=>$mensaje,'medalla'=>$medalla,'error'=>$error));
		}
	}
	
public function loadModel($id)
	{
		$model=  RSurfMarcador::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}